﻿@using SmartStore.Core.Domain.Directory;
@using Telerik.Web.Mvc.UI;
@using SmartStore.Web.Framework.UI;
@model CurrencyModel
@{
    var availableDomainEndings = Model.AvailableDomainEndings.Select(x => x.Value).ToArray();
    for (var i = 0; i < availableDomainEndings.Length; i++)
    {
        availableDomainEndings[i] = availableDomainEndings[i].EncodeJsString();
    }
    ViewBag.SerializedDomainEndings = string.Join(",", availableDomainEndings);
}

@if (!ViewData.ModelState.IsValid)
{
    @Html.ValidationSummary(false)
}

@Html.HiddenFor(model => model.Id)
@Html.SmartStore().TabStrip().Name("currency-edit").Style(TabsStyle.Material).Position(TabsPosition.Top).Items(x =>
{
	x.Add().Text(T("Admin.Common.Info").Text)
		//.Icon("fa fa-pencil-alt fa-lg fa-fw")
		.Content(TabInfo())
		.Selected(true);

	x.Add().Text(T("Admin.Common.Stores").Text)
		//.Icon("fa fa-globe fa-lg fa-fw")
		.Content(TabStores());

	//generate an event
	EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "currency-edit", this.Html, this.Model));
})

@helper TabInfo()
{
    <script type="text/javascript">
        $(document).ready(function () {
			
            $('#@(Html.FieldIdFor(x => x.RoundOrderItemsEnabled))').change(function () {
                $('tr.round-order-items').toggle($(this).is(':checked'));
            }).trigger('change');

            $('#@(Html.FieldIdFor(x => x.RoundOrderTotalEnabled))').change(function () {
                $('tr.round-order-total').toggle($(this).is(':checked'));
            }).trigger('change');

        });
    </script>

    var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures).ToList();
    @(Html.LocalizedEditor<CurrencyModel, CurrencyLocalizedModel>("currency-info-localized",
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].Name)
                </td>
                <td class="adminData">
					@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
					@Html.HiddenFor(model => model.Locales[item].LanguageId)

                    @Html.EditorFor(model => Model.Locales[item].Name)
                    @Html.ValidationMessageFor(model => model.Locales[item].Name)
                </td>
            </tr>
        </table>
        ,
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Name)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </td>
            </tr>
        </table>
    ))

<table class="adminContent">
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.CurrencyCode)
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.CurrencyCode)
            @Html.ValidationMessageFor(model => model.CurrencyCode)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.Rate)
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.Rate)
            @Html.ValidationMessageFor(model => model.Rate)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.DisplayLocale)
        </td>
        <td class="adminData">
            @Html.DropDownListFor(x => x.DisplayLocale, cultures.Select(x => 
                new SelectListItem { Value = x.IetfLanguageTag, Text = string.Format("{0} [{1}]", x.DisplayName, x.IetfLanguageTag) }))
            @Html.ValidationMessageFor(model => model.DisplayLocale)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.CustomFormatting)
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.CustomFormatting)
            @Html.ValidationMessageFor(model => model.CustomFormatting)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.DomainEndings)
        </td>
        <td class="adminData">
			@Html.ListBoxFor(model => model.DomainEndingsArray, Model.AvailableDomainEndings, new { multiple = "multiple", data_tags = "true" })
            @Html.ValidationMessageFor(model => model.DomainEndings)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.Published)
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.Published)
            @Html.ValidationMessageFor(model => model.Published)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.SmartLabelFor(model => model.DisplayOrder)
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.DisplayOrder)
            @Html.ValidationMessageFor(model => model.DisplayOrder)
        </td>
    </tr>
    
	@if (Model.Id > 0)
	{
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.CreatedOn)
			</td>
			<td class="adminData">
				<div class="form-control-plaintext">
					@Html.DisplayFor(model => model.CreatedOn)
				</div>
			</td>
		</tr>
	}

	<tr>
		<td class="adminTitle">
			@Html.SmartLabelFor(model => model.PrimaryStoreCurrencyStores)
		</td>
		<td class="adminData">
			@if (Model.PrimaryStoreCurrencyStores != null && Model.PrimaryStoreCurrencyStores.Count > 0)
			{
				foreach (var store in Model.PrimaryStoreCurrencyStores)
				{
					<a href="@store.Value">@store.Text.NaIfEmpty()</a><br />
				}
			}
			else
			{
				<div class="form-control-plaintext">
					@T("Common.Unspecified")
				</div>
			}
		</td>
	</tr>
	<tr>
		<td class="adminTitle">
			@Html.SmartLabelFor(model => model.PrimaryExchangeRateCurrencyStores)
		</td>
		<td class="adminData">
			@if (Model.PrimaryExchangeRateCurrencyStores != null && Model.PrimaryExchangeRateCurrencyStores.Count > 0)
			{
				foreach (var store in Model.PrimaryExchangeRateCurrencyStores)
				{
					<a href="@store.Value">@store.Text.NaIfEmpty()</a><br />
				}
			}
			else
			{
				<div class="form-control-plaintext">
					@T("Common.Unspecified")
				</div>
			}
		</td>
	</tr>
</table>

    <table class="adminContent">
        <tr>
            <td colspan="2">
                <div class="admin-config-group">
                    <div class="title">@T("Common.Round")</div>
                </div>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RoundOrderItemsEnabled)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RoundOrderItemsEnabled)
                @Html.ValidationMessageFor(model => model.RoundOrderItemsEnabled)
            </td>
        </tr>
        <tr class="round-order-items">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RoundNumDecimals)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RoundNumDecimals)
                @Html.ValidationMessageFor(model => model.RoundNumDecimals)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RoundOrderTotalEnabled)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RoundOrderTotalEnabled)
                @Html.ValidationMessageFor(model => model.RoundOrderTotalEnabled)
            </td>
        </tr>
        <tr class="round-order-total">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RoundOrderTotalDenominator)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.RoundOrderTotalDenominator, new List<SelectListItem>
                {
                    new SelectListItem { Text = 0.05.ToString("F"), Value = 0.05.ToString("F") },
                    new SelectListItem { Text = 0.10.ToString("F"), Value = 0.10.ToString("F") },
                    new SelectListItem { Text = 0.50.ToString("F"), Value = 0.50.ToString("F") },
                    new SelectListItem { Text = 1.00.ToString("F"), Value = 1.00.ToString("F") }
                }, new { @class = "autowidth", @style = "width: 150px" })
                @Html.ValidationMessageFor(model => model.RoundOrderTotalDenominator)
            </td>
        </tr>
        <tr class="round-order-total">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RoundOrderTotalRule)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.RoundOrderTotalRule, Model.RoundOrderTotalRule.ToSelectList())
                @Html.ValidationMessageFor(model => model.RoundOrderTotalRule)
            </td>
        </tr>
        <tr class="round-order-total">
            <td class="adminTitle">
            </td>
            <td class="adminData">
                
                @if (Model.RoundOrderTotalPaymentMethods.Any())
                {
                    <div class="alert alert-info">
                        <span>@T("Admin.Configuration.Currencies.PaymentMethodsEnabledRoundingList"):</span>
                        @foreach (var paymentMethod in Model.RoundOrderTotalPaymentMethods)
                        {
                            <span class="pr-1">
                                <a href="@Url.Action("Edit", "Payment", new { systemName = paymentMethod.Key })">@paymentMethod.Value</a>
                            </span>
                        }
                    </div>
                }
                else
                {
                    <div class="alert alert-warning">
                        @Html.Raw(T("Admin.Configuration.Currencies.NoPaymentMethodsEnabledRounding").Text.FormatInvariant(Url.Action("Providers", "Payment")))
                    </div>
                }
            </td>
        </tr>
    </table>
}

@helper TabStores()
{
	@Html.Partial("StoreSelector", Model)
}